package com.ruoyi.project.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.domain.SmProduct;
import com.ruoyi.project.system.mapper.SmCustomizedPriceMapper;
import com.ruoyi.project.system.mapper.SmProductMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.system.service.ISmProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 产品Service业务层处理
 */
@Service
public class SmProductServiceImpl implements ISmProductService {

    @Autowired
    private SmProductMapper smProductMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SmCustomizedPriceMapper smCustomizedPriceMapper;

    @Override
    public BigDecimal basePrice(Long userId, Integer productId, Integer roleId) {
        SmProduct smProduct = smProductMapper.selectSmProductById(productId);
        if (roleId == 2) {          // 区域队长
            return smProduct.getRegionPricing();
        } else if (roleId == 3) {   // 团队长
            return smProduct.getRegionalTeamPricing();
        } else if (roleId == 4) {   // 团员
            BigDecimal amount = smCustomizedPriceMapper.queryAmount(userMapper.queryParentId(userId), productId, "3-XJTC"); // MGZ TODO 2025/4/12: 增加算价
            if (amount == null) {
                return smProduct.getRegionalTeamPricing().add(smProduct.getBasePriceAdd());
            }
            return smProduct.getRegionalTeamPricing().add(amount);
        } else if (roleId == 5) {   // 集体账户
            return smProduct.getGroupAccountPricing();
        } else {                    // 普通账户
            return smProduct.getGeneralAccountPricing();
        }
    }

    @Override
    public BigDecimal promotionAdd(Long userId, Integer productId, Integer roleId) {
        BigDecimal amount = smCustomizedPriceMapper.queryAmount(userId, productId, roleId + "-TGSY");
        return amount != null ? amount : smProductMapper.selectSmProductById(productId).getPromotionAdd();
    }

    @Override
    public BigDecimal promotionPrice(Long userId, Integer productId, Integer roleId) {
        return basePrice(userId, productId, roleId).add(promotionAdd(userId, productId, roleId));
    }

    @Override
    public BigDecimal basePriceAdd(Long userId, Integer productId, Integer roleId) {
        SmProduct smProduct = smProductMapper.selectSmProductById(productId);
        if (roleId == 2) {          // 区域队长
            return smProduct.getRegionalTeamPricing().subtract(smProduct.getRegionPricing());
        } else if (roleId == 3) {   // 团队长
            BigDecimal amount = smCustomizedPriceMapper.queryAmount(userId, productId, roleId + "-XJTC"); // MGZ TODO 2025/4/12:增加算价
            if (amount == null) {
                return smProduct.getBasePriceAdd().setScale(2, BigDecimal.ROUND_DOWN);
            }
            return amount.setScale(2, BigDecimal.ROUND_DOWN);
        } else {
            return null;
        }
    }

    @Override
    public BigDecimal subordinatePrice(Long userId, Integer productId, Integer roleId) {
        SmProduct smProduct = smProductMapper.selectSmProductById(productId);
        if (roleId == 2) {          // 区域队长
            return smProduct.getRegionalTeamPricing();
        } else if (roleId == 3) {   // 团队长
            BigDecimal amount = smCustomizedPriceMapper.queryAmount(userId, productId, roleId + "-XJTC"); // MGZ TODO 2025/4/12:增加算价
            if (amount == null) {
                return smProduct.getRegionalTeamPricing().add(smProduct.getBasePriceAdd());
            }
            return smProduct.getRegionalTeamPricing().add(amount);
        } else {
            return null;
        }
    }

    /**
     * 查询产品
     *
     * @param id 产品主键
     * @return 产品
     */
    @Override
    public SmProduct selectSmProductById(Integer id) {
        return smProductMapper.selectSmProductById(id);
    }

    /**
     * 新增产品
     *
     * @param smProduct 产品
     * @return 结果
     */
    @Override
    public int insertSmProduct(SmProduct smProduct) {
        return smProductMapper.insertSmProduct(smProduct);
    }

    /**
     * 修改产品
     *
     * @param smProduct 产品
     * @return 结果
     */
    @Override
    public int updateSmProduct(SmProduct smProduct) {
        smProduct.setUpdateTime(DateUtils.getNowDate());
        return smProductMapper.updateSmProduct(smProduct);
    }

}
